// pages/member/rankingListSecond/rankingListSecond.js
import {payKeyboardAsync} from "../../../../mycomponent/payKeyboard/PayKeyboardAsync";
var app = getApp();
Page({
  behaviors: [payKeyboardAsync],
  /**
	 * 页面的初始数据
	 */
  data: {
    popupSpecs: false,
    language: '',
    usable: 0.0,
    sure_usable: "",
    radio: "",
    Withdrawaltext: '提现',
    isclick:true,
    balance_withdraw_status:'0',
    withdraw_status:0,
    isConfirmBtn: false,//是否显示提现按钮
    final_proportion_rate:0,
    point:"积分"
  },

  /**
	 * 生命周期函数--监听页面加载
	 */
  onLoad: function(options) {
    this.usable = 0.0;
    this.sure_usable = "";
    // this.toi = this.fun.getKeyByI();
    //获取数据
    // this.getStoreInfo();
    this.customizeIncome();
    try {
      this.setData({point:wx.getStorageSync('integral'),balance:wx.getStorageSync('balance')})
    } catch (error) {
      
    }
  },

  /**
	 * 生命周期函数--监听页面初次渲染完成
	 */
  onReady: function() {
    let language = wx.getStorageSync('langIndex');
    this.setData({ 'language': language.en});
  },

  /**
	 * 生命周期函数--监听页面显示
	 */
  onShow: function() {
    this.getStoreInfo();
    this.data.isclick = true;
  },

  /**
	 * 生命周期函数--监听页面隐藏
	 */
  onHide: function() {

  },

  /**
	 * 生命周期函数--监听页面卸载
	 */
  onUnload: function() {

  },

  /**
	 * 页面相关事件处理函数--监听用户下拉动作
	 */
  onPullDownRefresh: function() {

  },

  /**
	 * 页面上拉触底事件的处理函数
	 */
  onReachBottom: function() {

  },

  /**
	 * 用户点击右上角分享
	 */
  onShareAppMessage: function() {

  },
  getStoreInfo() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.love.Frontend.Modules.Love.Controllers.withdraw.page");
    app._getNetWork({
      url: urlStr,
      success: (res) => {
        let resData = res.data;
        if (resData.result === 1) {
          that.setData({
            Data: resData.data,
            withdraw_status :resData.data.withdraw_status,
            integralShow: resData.data.integral_withdraw_status,
            balance_withdraw_status:resData.data.balance_withdraw_status,
            coin: Number(that.data.sure_usable) * Number(resData.data.withdraw_integral_scale) * Number(resData.data
              .integral_withdraw_proportion /
              100),
            final_proportion_rate:resData.data.final_proportion_rate
          });
          if(resData.data.balance_withdraw_status==1){
            console.log("执行3");
            this.setData({
              radio:"4"
            });
          }
          if(resData.data.integral_withdraw_status==1){
            console.log("执行2");
            this.setData({
              radio:"2"
            });
          }
          if(resData.data.withdraw_status==1){
            this.setData({
              radio:"1"
            });
          }
          this.isShowConfirmBtn();
        } else {
          wx.showToast({
            title: resData.msg,
            icon: "none"
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  isShowConfirmBtn() {
    if (this.data.withdraw_status || this.data.integralShow || this.data.balance_withdraw_status == "1" || this.data.Data.we_chat_withdraw_status == "1") {
      this.setData({
        isConfirmBtn: true
      })
    }
  },
  async confirm() {
    if(!this.data.isclick){
      wx.showToast({
        title: '请勿重复点击',
        icon: 'none'
      });
      return;			
    }
    this.data.isclick = false;
    if (this.data.sure_usable == undefined || this.data.sure_usable <= 0) {
      wx.showToast({
        title: `${this.data.Withdrawaltext}数值不可低于0`,
        icon: "none"
      });
      this.data.isclick = true;
      return;
    }

    if (!this.data.radio) {
      this.data.isclick = true;
      return wx.showToast({
        title: '请选择提现类型',
        icon: "none"
      });
    }

    if(this.data.radio == '14'&&!this.data.Data.lsp_wallet_site){
      this.data.isclick = true;
      wx.showModal({
        content: '暂无钱包地址',
        confirmText: '去设置',
        success (res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/packageI/walletSiteBind/walletSiteBind' //钱包app绑定钱包地址【#4203】
            })
          }
        }
      });
      return;
    }

    let json = {
      change_value: this.data.sure_usable,
      withdraw_type:  this.data.radio
    }

    if (this.data.radio == '14'&&this.data.Data.need_password) {
      let pass = await this.getPayKeyboardPassword();//弹出输入密码框
      json.password = pass;
    }

    let urlStr = app.getNetAddresss("plugin.love.Frontend.Modules.Love.Controllers.withdraw.index");
    app._getNetWork({
      url: urlStr,
      data: json,
      success: (res) => {
        console.log(res, '数据');
        if (res.data.result === 1 && res.data.data.withdraw_type == "1") {
          // 跳转到提现页面
          wx.navigateTo({
            url: '/packageA/member/withdrawal/withdrawal'
          });
        } else if(res.data.result === 1 && res.data.data.withdraw_type == "4"){
          setTimeout(()=>{
            console.log("执行");
            wx.navigateBack();
          },1000);
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
				
        }else {
          if(res.data.msg == 'need register'){
            if(this.data.radio == '10' ||this.data.radio == '11'|| this.data.radio == '12' ){ //好灵工
              wx.navigateTo({
                url: '/packageI/worker/type/type?path=loveWithdrawals',
              })
            }
            return
          }
          
          this.data.isclick = true;
          wx.showToast({
            title: res.data.msg,
            icon: "none"
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },
  payKeyboardComplete(evt) {
    this.setData({
      popupSpecs: false
    });
    payKeyboardAsync.complete(evt.detail);
  },
  getPayKeyboardPassword() {
    this.setData({
      popupSpecs: true
    });
    return new Promise((resove, reject) => {
      payKeyboardAsync.addCompleteFn((pass) => {
        let urlStr = app.getNetAddresss("payment.password.check");
        urlStr += "&password=" + pass;
        app._getNetWork({
          url: urlStr,
          success: (resdata) => {
            var res = resdata.data;
            if (res.result == 1) {
              resove(pass);
            } else {
              this.data.isclick = true;
              wx.showModal({
                title: "提示",
                content: res.msg,
                success(restext) {
                  if (restext.confirm) {
                    if (res.data.code == 2002) {
                      // 用户未设置支付密码
                      // 去设置密码
                      wx.navigateTo({
                        url: "/packageA/member/set_balance_password/set_balance_password",
                      });
                    }
                  }
                },
              });
            }
          },
          fail: (res) => {
            console.log(res);
          },
        });
        
      });
    });
  },

  // 输入框改变的时候触发
  onChange(val) {
    this.setData({
      sure_usable: val.detail
    });
  },
  //自定义提现收入语言
  customizeIncome() {
    try {
      var value = wx.getStorageSync('mailLanguage');
      if (value) {
        let mailLanguage = JSON.parse(value);
        // Do something with return value
        this.setData({
          Withdrawaltext: mailLanguage.income.name_of_withdrawal
        });
        wx.setNavigationBarTitle({
          title: mailLanguage.income.name_of_withdrawal ? mailLanguage.income.name_of_withdrawal : '提现'
        });
      }
    } catch (e) {
      // Do something when catch error
    }
  },
  onChangeRadio(val) {
    console.log(val);
    this.setData({
      radio:val.detail,
      discount_method: val.detail
    });
  },
});
